package top.fzqblog.music.activity;

import android.animation.Animator;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ViewAnimationUtils;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.nightonke.boommenu.BoomButtons.ButtonPlaceEnum;
import com.nightonke.boommenu.BoomButtons.OnBMClickListener;
import com.nightonke.boommenu.BoomButtons.TextInsideCircleButton;
import com.nightonke.boommenu.BoomMenuButton;
import com.nightonke.boommenu.ButtonEnum;
import com.nightonke.boommenu.Piece.PiecePlaceEnum;
import com.nightonke.boommenu.Util;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import top.fzqblog.app.FirstApplication;
import top.fzqblog.firstapp.R;
import top.fzqblog.music.entity.TabEntity;
import top.fzqblog.music.event.PlayEvent;
import top.fzqblog.music.event.ScanEvent;
import top.fzqblog.music.event.SongChangeEvent;
import top.fzqblog.music.fragment.ListFragment;
import top.fzqblog.music.fragment.MainFragment;
import top.fzqblog.music.model.Song;
import top.fzqblog.music.service.MediaService;
import top.fzqblog.music.util.MusicUtils;
import top.wefor.circularanim.CircularAnim;

import static top.fzqblog.utils.Constants.TAG;

/**
 * Created by 抽离 on 2017/10/12 0012.
 */

public class MusicMain extends AppCompatActivity {

    private String[] mTitles = {"列表", "更多"};
    private int[] mIconUnselectIds = {
            R.mipmap.tab_home_unselect, R.mipmap.tab_more_unselect};
    private int[] mIconSelectIds = {
            R.mipmap.tab_home_select, R.mipmap.tab_more_select};

    private ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();

    private ArrayList<Fragment> fragments = new ArrayList<>();

    @BindView(R.id.ctl)
    public CommonTabLayout commonTabLayout;

    @BindView(R.id.viewPager)
    public ViewPager viewPager;

    @BindView(R.id.playbar_img)
    public ImageView playbar_img;

    @BindView(R.id.playbar_info)
    public TextView playbar_info;

    @BindView(R.id.playbar_singer)
    public TextView playbar_singer;

    @BindView(R.id.bmb)
    public BoomMenuButton bmb;

    TextInsideCircleButton.Builder builder1;

    TextInsideCircleButton.Builder builder2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_main);
        ButterKnife.bind(this);
        initData();
        initFloatButton();
        initEventBus();
        viewPager.setAdapter(new MyPagerAdpater(getSupportFragmentManager()));
        commonTabLayout.setTabData(tabEntities);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                Log.i(TAG, "onTabSelect: " + position);
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                Log.i(TAG, "onPageSelected: " + position);
                switch (position){
                    case 1:
                        bmb.clearBuilders();
                        bmb.setPiecePlaceEnum(PiecePlaceEnum.DOT_1);
                        bmb.setButtonPlaceEnum(ButtonPlaceEnum.SC_1);
                        bmb.addBuilder(builder2);
                        break;
                    case 0:
                        bmb.clearBuilders();
                        bmb.setPiecePlaceEnum(PiecePlaceEnum.DOT_2_1);
                        bmb.setButtonPlaceEnum(ButtonPlaceEnum.SC_2_1);
                        bmb.addBuilder(builder1);
                        bmb.addBuilder(builder2);
                        break;
                }
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        commonTabLayout.setCurrentTab(0);
        viewPager.setCurrentItem(0);
        initService();
    }

    private void initService() {
        startService(new Intent(this, MediaService.class));
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSongChange(SongChangeEvent songChangeEvent){
//        Toast.makeText(this, "event:" + songChangeEvent, Toast.LENGTH_SHORT).show();
        Song song = songChangeEvent.getSong();
        switch (songChangeEvent.getFlag()){
            case 1:
                control.setImageResource(R.mipmap.playbar_btn_play);//设置为待播放图标
                control.setTag("play");
                break;
            case 2:
                control.setImageResource(R.mipmap.pause_btn);//设置为播放状态
                control.setTag("pause");
                break;
            case 3:
                //下一首只需要设置歌曲信息
//                Toast.makeText(this, "song:"+song, Toast.LENGTH_SHORT).show();
                if(song.net){
                    Glide
                            .with(this)
                            .load(song.getNetImg())
                            .error(R.drawable.default_album)
                            .crossFade(700)
                            .into(playbar_img);
                }else{
                    playbar_img.setImageBitmap(MusicUtils.getArtwork(this, song.getId(), song.getAlbumId(), true, false));
                }
                playbar_info.setText(song.getSong());
                playbar_singer.setText(song.getSinger());
                break;
            case 4:
                //直接点击歌曲列表需要同时设置图标和歌曲
                control.setTag("pause");
                control.setImageResource(R.mipmap.pause_btn);//设置为播放状态
                if(song.net){
                    Glide
                            .with(this)
                            .load(song.getNetImg())
                            .error(R.drawable.default_album)
                            .crossFade(700)
                            .into(playbar_img);
                }else{
                    playbar_img.setImageBitmap(MusicUtils.getArtwork(this, song.getId(), song.getAlbumId(), true, false));
                }
                playbar_info.setText(song.getSong());
                playbar_singer.setText(song.getSinger());
                break;
        }
    }

    @BindView(R.id.control)
    public ImageView control;

    @BindView(R.id.play_next)
    public ImageView playNext;

    @OnClick({R.id.control, R.id.play_next})
    public void onControlClick(ImageView imageView){
//        Toast.makeText(this, "tag:"+ control.getTag(), Toast.LENGTH_SHORT).show();
        switch (imageView.getId()){
            case R.id.control:
                if("play".equals(control.getTag())){
                    EventBus.getDefault().post(new PlayEvent(PlayEvent.PlayAction.RESUME));
//                    control.setImageResource(R.mipmap.pause_btn);
//                    control.setTag("pause");
                }else{
                    EventBus.getDefault().post(new PlayEvent(PlayEvent.PlayAction.STOP));
//                    control.setImageResource(R.mipmap.playbar_btn_play);
//                    control.setTag("play");
                }
                break;
            case R.id.play_next:
                EventBus.getDefault().post(new PlayEvent(PlayEvent.PlayAction.NEXT));
                break;
        }
    }


    private void initEventBus() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initFloatButton() {
        bmb.clearBuilders();
        bmb.setButtonEnum(ButtonEnum.TextInsideCircle);
        bmb.setPiecePlaceEnum(PiecePlaceEnum.DOT_2_1);
        bmb.setButtonPlaceEnum(ButtonPlaceEnum.SC_2_1);

        builder1 = new TextInsideCircleButton.Builder()
                .normalImageRes(R.mipmap.scan)
                .isRound(false)
                .normalColor(Color.rgb(0,171,191))
                .shadowCornerRadius(Util.dp2px(10))
                .buttonCornerRadius(Util.dp2px(10))
                .listener(new OnBMClickListener() {
                    @Override
                    public void onBoomButtonClick(int i) {
//                        Toast.makeText(MusicMain.this, "index" + i, Toast.LENGTH_SHORT).show();
                        EventBus.getDefault().post(new ScanEvent(1));
                    }
                });

        builder2 = new TextInsideCircleButton.Builder()
                .normalImageRes(R.mipmap.search)
                .isRound(false)
                .normalColor(Color.rgb(253,152,0))
                .shadowCornerRadius(Util.dp2px(10))
                .buttonCornerRadius(Util.dp2px(10))
                .listener(new OnBMClickListener() {
                    @Override
                    public void onBoomButtonClick(int i) {
                        startActivity(new Intent(MusicMain.this, MusicSearch.class));
//                        Toast.makeText(MusicMain.this, "index" + i, Toast.LENGTH_SHORT).show();
                    }
                });
        bmb.setNormalColor(getResources().getColor(R.color.colorTheme));
        bmb.addBuilder(builder1);
        bmb.addBuilder(builder2);
    }

    private void initData() {
        for (int i = 0; i < mTitles.length; i++) {
            tabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        fragments.add(new ListFragment());
        fragments.add(new MainFragment());
    }


    class MyPagerAdpater extends FragmentPagerAdapter{

        public MyPagerAdpater(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}

